package com.lm.itextpdf.handler;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;

public class HeaderFooterHandler implements IEventHandler {
    @Override
    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfDocument pdfDoc = docEvent.getDocument();
        PdfPage page = docEvent.getPage();

        float x = page.getPageSize().getWidth() / 2;
        float yHeader = page.getPageSize().getTop() - 20;
        float yFooter = 20;

        new com.itextpdf.layout.Canvas(new com.itextpdf.kernel.pdf.canvas.PdfCanvas(page), page.getPageSize())
                .showTextAligned("Header - Page " + pdfDoc.getPageNumber(page), x, yHeader, TextAlignment.CENTER)
                .showTextAligned("Footer - Page " + pdfDoc.getPageNumber(page), x, yFooter, TextAlignment.CENTER);
    }
}
